package com.hrd.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hrd.common.util.bean.ServiceResult;
import com.hrd.datasource.DatabaseContextHolder;
import com.hrd.mybatis.bean.auto.*;
import com.hrd.mybatis.bean.ext.InvModel;
import com.hrd.service.CommonService;
import com.hrd.service.WarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: wangpeng
 * @Date: 2022/1/12 14:41
 * @Description:
 */
@Service
public class WarehouseServiceImpl extends BaseServiceImpl implements WarehouseService {
    @Autowired
    private CommonService commonService;

    @Override
    public void synchronizationWarehouseInfo() {
        //切换数据源到sqlserver
        DatabaseContextHolder.clearCustomerType();
        DatabaseContextHolder.setCustomerType(DatabaseContextHolder.DATA_SOURCE_SQL_SERVER);
        //查询和锐德数据库 仓库信息
        TDCItemExample example = new TDCItemExample();
        TDCItemExample.Criteria criteria = example.createCriteria();
        criteria.andFitemclassidEqualTo(1562);
        List<TDCItem> tdcItems = this.tdcItemMapper.selectByExample(example);
        if (tdcItems != null) {
            DatabaseContextHolder.clearCustomerType();
            DatabaseContextHolder.setCustomerType(DatabaseContextHolder.DATA_SOURCE_MYSQL);
            for (TDCItem tdcItem : tdcItems) {

                try {
                    HrdWarehouseRelationExample hrdWarehouseRelationExample = new HrdWarehouseRelationExample();
                    hrdWarehouseRelationExample.createCriteria().andHrdWarehouseIdEqualTo(tdcItem.getFid());
                    int count = this.hrdWarehouseRelationMapper.countByExample(hrdWarehouseRelationExample);
                    if (count > 0) {
                        continue;
                    }
                    //插入联信数据库
                    HrdWarehouseRelation hrdWarehouseRelation = new HrdWarehouseRelation();
                    hrdWarehouseRelation.setHrdWarehouseId(tdcItem.getFid());
                    hrdWarehouseRelation.setHrdWarehouseName(tdcItem.getFname());
                    this.hrdWarehouseRelationMapper.insert(hrdWarehouseRelation);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }

    @Override
    public ServiceResult<List<HrdWarehouseRelation>> list(HrdWarehouseRelation warehouse) {
        DatabaseContextHolder.clearCustomerType();
        DatabaseContextHolder.setCustomerType(DatabaseContextHolder.DATA_SOURCE_MYSQL);
        ServiceResult<List<HrdWarehouseRelation>> result = new ServiceResult<>();
        HrdWarehouseRelationExample hrdWarehouseRelationExample = new HrdWarehouseRelationExample();
        HrdWarehouseRelationExample.Criteria criteria = hrdWarehouseRelationExample.createCriteria();
        if (warehouse.getHrdWarehouseId() != null) {
            criteria.andHrdWarehouseIdEqualTo(warehouse.getHrdWarehouseId());
        }

        List<HrdWarehouseRelation> hrdWarehouseRelations = this.hrdWarehouseRelationMapper.selectByExample(hrdWarehouseRelationExample);
        result.setSuccess(true);
        result.setResult(hrdWarehouseRelations);
        return result;
    }

    @Override
    public HrdWarehouseRelation getInfo(Integer id) {
        DatabaseContextHolder.clearCustomerType();
        DatabaseContextHolder.setCustomerType(DatabaseContextHolder.DATA_SOURCE_MYSQL);
        return this.hrdWarehouseRelationMapper.selectByPrimaryKey(id);
    }

    public List<JSONObject> getJdwarehouse() {
        List<JSONObject> resultList = new ArrayList<>();
        DatabaseContextHolder.clearCustomerType();
        DatabaseContextHolder.setCustomerType(DatabaseContextHolder.DATA_SOURCE_MYSQL);
        HrdWarehouseRelationExample example = new HrdWarehouseRelationExample();
        example.createCriteria().andJdWarehouseIdIsNotNull();
        List<HrdWarehouseRelation> hrdWarehouseRelations = this.hrdWarehouseRelationMapper.selectByExample(example);
        HashMap<String,HrdWarehouseRelation> map = new HashMap<>();
        for (HrdWarehouseRelation hrdWarehouseRelation : hrdWarehouseRelations) {
            map.put(hrdWarehouseRelation.getJdWarehouseId(),hrdWarehouseRelation);
        }
        //获取token
        String token = commonService.getAccessToken() == null ? "" : commonService.getAccessToken().getResult();

        ServiceResult<JSONArray> baseData = commonService.getBaseData(token);
        if (baseData!=null && baseData.getResult()!=null){
            JSONArray result = baseData.getResult();
            for (Object o : result) {
                JSONObject jsonObject = (JSONObject) o;
                resultList.add(jsonObject);
            }
        }
        return resultList;
    }

    public List<JSONObject> getSpace(String groupid) {
        List<JSONObject> resultList = new ArrayList<>();
        //获取token
        String token = commonService.getAccessToken() == null ? "" : commonService.getAccessToken().getResult();
        //获取套账
        ServiceResult<JSONArray> spaceData = commonService.getSpaceData(token, groupid);
        if (spaceData!=null && spaceData.getResult()!=null){
            JSONArray result = spaceData.getResult();
            for (Object o : result) {
                JSONObject jsonObject = (JSONObject) o;
                resultList.add(jsonObject);
            }
        }
        return resultList;
    }



    @Override
    public ServiceResult<HrdWarehouseRelation> update(HrdWarehouseRelation param) {
        ServiceResult<HrdWarehouseRelation> result = new ServiceResult<>();
        DatabaseContextHolder.setCustomerType(DatabaseContextHolder.DATA_SOURCE_MYSQL);
        HrdWarehouseRelation hrdWarehouseRelation = this.hrdWarehouseRelationMapper.selectByPrimaryKey(param.getId());
        hrdWarehouseRelation.setJdWarehouseId(param.getJdWarehouseId());
        hrdWarehouseRelation.setJdWarehouseNumber(param.getJdWarehouseNumber());
        hrdWarehouseRelation.setJdSpaceId(param.getJdSpaceId());
        hrdWarehouseRelation.setJdSpaceNumber(param.getJdSpaceNumber());
        this.hrdWarehouseRelationMapper.updateByPrimaryKey(hrdWarehouseRelation);
        result.setSuccess(true);
        result.setResult(hrdWarehouseRelation);
        return result;
    }
}
